import {Base} from 'base.js'

class Shopcart extends Base{
  constructor(key){
    super()
    this.shopcartKey = key
    this.shopcartArr = []
  }

  getShopcartDetail(selectedAll){
    this.getLocalShopcart()
    var shopcartArray = this.shopcartArr
    // 选中的书
    var data = this._getSelectedBookData( selectedAll)
    // 全部的书
    data.shopcartArray = shopcartArray
    data.selectedAll = data.selectedBookNum == shopcartArray.length && shopcartArray.length > 0 ? true: false
    return data
  }

  _getSelectedBookData(selectedAll){
    var shopcartArray = this.shopcartArr
    // 多少种书
    var selectedBookNum = 0 
    var selectedBookPrice = 0
    // 所有的数量
    var selectedBookNumAll = 0
    var selectedBooks = []
    let multi = 100
    if (selectedAll) {
      selectedBooks = shopcartArray
      selectedBookNum = shopcartArray.length
      
      shopcartArray.forEach(item => {
        selectedBookPrice += item.count * multi * Number(item.price) * multi
        selectedBookNumAll += item.count
      })
    } else{
      shopcartArray.forEach(item => {
        if(item.selected){
          selectedBookNum++
          selectedBookNumAll += item.count
          selectedBookPrice += item.count * multi * Number(item.price) * multi
          selectedBooks.push(item)
        }
      })
      }

    return {
      selectedBookArray :selectedBooks,
      selectedBookNum: selectedBookNum,
      orderPrice: selectedBookPrice / (multi * multi),
      selectedBookNumAll: selectedBookNumAll
    }
  }


  // 下单成功 更新购物车
    // 清空selectedBook 将selected == true 的删除
    // 将selected == false 的保存
  updataShopcart() {
    this.getLocalShopcart()
    var shopcartArr = this.shopcartArr
    var bookArray = []
    shopcartArr.forEach(item => {
      if(!item.selected) {
        bookArray.push(item)
      }
    })
    this.updateLocalShopcart(bookArray)
  }


// 库存不足
  lowStock(bookStatusArray){
    bookStatusArray.forEach((item)=>{
      if (!item.haveStock && item.stock > 0){
        // 将库存不足的商品数量改为库存量
        item.count = item.stock
      }
      item.selected = true
      delete item.totalPrice
      delete item.stock
      delete item.haveStock
    })

    this.getLocalShopcart()
    var shopcartArr = this.shopcartArr
    shopcartArr.forEach(item=>{
      if(!item.selected){
        bookStatusArray.push(item)
        // console.log(bookStatusArray)
      }
    })
    this.updateLocalShopcart(bookStatusArray)
  }

// 获取缓存的购物车信息
  getLocalShopcart(){
    var shopcartArray = wx.getStorageSync(this.shopcartKey)
    if(!shopcartArray){
      shopcartArray = []
    }
    this.shopcartArr = shopcartArray
  }

// 更新缓存
  updateLocalShopcart(data){
    wx.setStorageSync(this.shopcartKey, data)
  }

//改变数量 
  changeNum(bookID, base) {
    // 获取购物车缓存
    this.getLocalShopcart()
    var shopcartArr = this.shopcartArr
    // 遍历购物车
    var result = {
      data:{},
      ok:false
    }
    var index = this._bookInShopcart(bookID, "id")
    if(index != -1 ){
      var bookNum = shopcartArr[index].count * 1 + base
      if (bookNum > 0) {
        shopcartArr[index].count = bookNum
        this.updateLocalShopcart(shopcartArr)
        result.data = this.getShopcartDetail(false)
        result.ok = true
      } else {
        // 改了商品数量 会小于等于0  不能改
        // 设置提醒
        this.toast("不能再减少了", "none")
      }
    }
    return result
  }

  // 删除
  deleteOne(bookID, selectedAll){
    var result = {
      ok:false,
      data:{}
    }
    this.getLocalShopcart()
    var shopcartArray = this.shopcartArr
    var index = this._bookInShopcart(bookID, "id")
    if(index != -1){
      shopcartArray.splice(index, 1)
      this.updateLocalShopcart(shopcartArray)
      result.data = this.getShopcartDetail(selectedAll)
      result.ok = true
    }
    return result
  }




  // 选中 或者 取消选中 某个商品
  changeSelectStatus(bookID){
    // 获取购物车缓存
    this.getLocalShopcart()
    var shopcartArr = this.shopcartArr
    // 遍历购物车
    var index = this._bookInShopcart(bookID, "id")
    if(index != -1){
      var selectStatus = shopcartArr[index].selected
      shopcartArr[index].selected = !selectStatus
      this.updateLocalShopcart(shopcartArr)
      return this.getShopcartDetail(false)
    }
  }

  // 全选 购物车
  selectAll(selectAll) {
    // 获取购物车缓存
    this.getLocalShopcart()
    var shopcartArr = this.shopcartArr
    // 选中
    shopcartArr.forEach(item => {
      item.selected = selectAll
    })

    // 更新缓存
    this.updateLocalShopcart(shopcartArr)
    return this.getShopcartDetail(selectAll)
  }

    /*
    **有没有这个商品
    **没有 新增
    **有  增加数量
    **更新缓存
    */
    // book的价格直接显示  book.price
  addToCart(book, num){
    // 获取缓存
    this.getLocalShopcart()
    var shopcartArr = this.shopcartArr
    var index = this._bookInShopcart(book.id, "id")
    // 书本未加入过购物车
    if (index == -1){
      book.count = num
      book.selected = true
      // 将这本书放到购物车列表的最前面
      shopcartArr.unshift(book)
    }
    // 书本已加入过购物车 改数量
    else{
      book.count = num + shopcartArr[index].count
      book.selected = true
      // 删除这本书
      shopcartArr.splice(index,1)
      // 将这本书放到购物车列表的最前面
      shopcartArr.unshift(book)
    }
    // 更新缓存
    this.updateLocalShopcart(shopcartArr)
  }

// 检查book在缓存   改变数量
  checkBookInCartByISBN(isbn){
    this.getLocalShopcart()
    var index = this._bookInShopcart(isbn, "isbn")
    if(index == -1){
      return false
    } else {
      var shopcartArray = this.shopcartArr
      shopcartArray[index].count += 1
      this.updateLocalShopcart(shopcartArray)
      return true
    }
  }

// 书本已经存在购物车？
  _bookInShopcart(value, key) {
    var shopcartArr = this.shopcartArr
    var index = -1
      for (let i = 0; i < shopcartArr.length; i++) {
        if (shopcartArr[i][key] == value) {
          index = i
          break
        }
    } 
    return index
  }

// isbn 查找书
  checkBookInDB(isbn,callback) {
    var url = "book?isbn=" + isbn
    var that = this
    var params = {
      url: url,
      fn: function(res){
        callback && callback(res)
      }
    }
    this.request(params)
  }
}

export {Shopcart}